package com.ruoyi.web.controller.app;

import com.ruoyi.app.domain.bo.AppTaskBo;
import com.ruoyi.app.domain.bo.AppTeamApplyBo;
import com.ruoyi.app.domain.bo.AppTeamListBo;
import com.ruoyi.app.domain.vo.AppTaskVo;
import com.ruoyi.app.domain.vo.AppTeamApplyVo;
import com.ruoyi.app.domain.vo.AppTeamListVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppTaskService;
import com.ruoyi.app.service.IAppTeamApplyService;
import com.ruoyi.app.service.IAppTeamListService;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.TaskConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * APP任务记录
 *
 * @author heike
 * @date 2025-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/task")
public class AppTaskController extends BaseController {

    private final IAppTaskService iAppTaskService;
    private final IAppUserService appUserService;
    private final IAppTeamListService appTeamListService;
    private final IAppTeamApplyService appTeamApplyService;

    /**
     * 查询APP任务记录列表
     */
    @GetMapping("/list")
    public R<List<AppTaskVo>> list() {
        AppTaskBo appTaskBo = new AppTaskBo();
        appTaskBo.setStatus(Constants.SUCCESS);
        appTaskBo.setType(Constants.SUCCESS);
        List<AppTaskVo> taskVos = iAppTaskService.queryList(appTaskBo);
        taskVos.forEach(e -> {
            String down = Constants.SUCCESS;
            if (e.getKeyName().equals(TaskConstants.userCert)) {
                AppUserVo appUserVo = appUserService.queryById(getUserId());
                if (StringUtils.isNotBlank(appUserVo.getCode())) {
                    down = Constants.FAIL;
                }
            } else if (e.getKeyName().equals(TaskConstants.invite)) {
                List<AppUserVo> appUserVos = appUserService.queryByPid(getUserId());
                if (!appUserVos.isEmpty()) {
                    down = Constants.FAIL;
                }
            } else if (e.getKeyName().equals(TaskConstants.complete)) {
                //查询用户发布的所有活动
                AppTeamListBo appTeamListBo = new AppTeamListBo();
                appTeamListBo.setUserId(getUserId());
                appTeamListBo.setStatus(Constants.THREE);
                List<AppTeamListVo> list = appTeamListService.queryList(appTeamListBo);
                for (AppTeamListVo k : list) {
                    //判断活动是否有人参加
                    AppTeamApplyBo appTeamApplyBo = new AppTeamApplyBo();
                    appTeamApplyBo.setTeamId(k.getId());
                    appTeamApplyBo.setStatus(Constants.FAIL);
                    List<AppTeamApplyVo> teamApplyVos = appTeamApplyService.queryList(appTeamApplyBo);
                    if (!teamApplyVos.isEmpty()) {
                        down = Constants.FAIL;
                        break;
                    }
                }
            } else {
                String key = TaskConstants.TASK + e.getKeyName() + ":" + getUserId();
                Integer count = RedisUtils.getCacheObject(key);
                if (count != null) {
                    down = Constants.FAIL;
                }
            }
            e.setDown(down);
        });
        return R.ok(taskVos);
    }

    /**
     * 获取APP任务记录详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<AppTaskVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iAppTaskService.queryById(id));
    }
}
